package WWW::BBB::API::Response;

our $VERSION = '0.01';

use strict;
use warnings;

use XML::Simple;

my %fields = ( xml	=> undef,);

sub new {
	my ($proto,%options) = @_;
	my $class = ref($proto) || $proto;
	my $self = {%fields};
	while (my ($key,$value) = each(%options)) {
		if (exists($fields{$key})) {
			$self->{$key} = $value if (defined $value);
		} else {
			die ref($class) . "::new: invalid option '$key'\n";
		}
	}
	#foreach (keys(%fields)) {
	#die ref($class) . "::new: must specify value for $_" 
	#	if (!defined $self->{$_});
	#}
	bless $self, $class;
	$self->_init();
	return $self;
}

sub _init {
	my $s	= shift;
	$s->parse_xml if ($s->xml);
}

sub parse_xml {
	my $s	= shift;
	my $obj	= XMLin($s->xml);

	$s->$_($obj->{$_}) foreach (qw/returncode messageKey message/);

	# if null, XMLin traslate it as an empty hashref
	$s->messageKey('') if (ref($s->messageKey) eq 'HASH');
	$s->message('') if (ref($s->message) eq 'HASH');
}

sub xml			{ my $s = shift; 
	if (@_) { $s->{&_pn} = shift; $s->parse_xml; } return $s->{&_pn}; }
*returncode = \&result; 
sub result		{ my $s = shift; if (@_) { $s->{&_pn} = shift; } return $s->{&_pn}; }
sub messageKey	{ my $s = shift; if (@_) { $s->{&_pn} = shift; } return $s->{&_pn}; }
sub message		{ my $s = shift; if (@_) { $s->{&_pn} = shift; } return $s->{&_pn}; }


sub _pn  { (split(/::/,(caller(1))[3]))[-1] }

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

WWW::BBB::API - Perl interface to BigBlueButton API system

=head1 SYNOPSIS

  use WWW::BBB::API;
  my $bbb = new WWW::BBB::API(salt => $salt, host => $host);
  my $meetings = $bbb->getMeetings;


=head1 DESCRIPTION


=head1 AUTHOR

Emiliano Bruni, E<lt>info@ebruni.it<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010 by Emiliano Bruni

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.


=cut
